package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
	. "web_api_gin/controller"
	"web_api_gin/core"
	"web_api_gin/dto"
	"web_api_gin/entity"
	"web_api_gin/service"
)

var UserApi = UserController{}

type UserController struct {
}

// Current 获取当前用户信息
func (c *UserController) Current(ctx *gin.Context) {
	userData, _, err := service.SysUser.AuthorizationGetUserData(ctx)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	if userData == nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, "用户不存在")
		return
	}
	userRoleNameList, userPermNameList, err := service.SysUser.GetAdminUserConf(ctx, userData, false)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, "角色列表异常")
		return
	}
	Success(ctx, gin.H{
		"user":     userData,
		"roleList": userRoleNameList,
		"permList": userPermNameList,
	})
}

// 更新当前用户信息
func (c *UserController) Update(ctx *gin.Context) {
	var form *entity.SysUsers
	if err := ctx.Bind(&form); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//设置表单数据，方便保存日志
	ctx.Set("formData", form)
	if err := service.SysUser.AuthorizationUpUserData(ctx, form); err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	Success(ctx, gin.H{})
}

// 更新密码
func (c *UserController) ResetPwd(ctx *gin.Context) {
	var form *dto.SysUserResetPwdRequest
	if err := ctx.Bind(&form); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//设置表单数据，方便保存日志
	ctx.Set("formData", form)
	if err := service.SysUser.AuthorizationResetPwd(ctx, form); err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	Success(ctx, gin.H{})
}

// GetConfig 获取用户配置
func (c *UserController) GetConfig(ctx *gin.Context) {
	userData, _, err := service.SysUser.AuthorizationGetUserData(ctx)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	if userData == nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, "用户不存在")
		return
	}
	_, userPermNameList, err := service.SysUser.GetAdminUserConf(ctx, userData, false)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, "角色列表异常")
		return
	}
	menuData, err := service.SysMenu.GetHasPermMenuList(ctx, "admin", userPermNameList)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, "菜单获取异常")
		return
	}
	Success(ctx, gin.H{
		//"user":           userData,
		//"roleList":       userRoleNameList,
		//"permissionList": userPermNameList,
		"menuData": menuData,
	})
}

//获取文章列表
func (c UserController) Articles(ctx *gin.Context) {
	var list []dto.SfyArticle
	for i := 0; i < 20; i++ {
		list = append(list, dto.SfyArticle{
			Id:          uint32(i),
			Title:       "测试模拟数据文章标题:" + strconv.Itoa(i),
			Avatar:      "https://file.suofeiya.com.cn/v/dc2cb8aa-c9d5-41f1-8a88-ea47d7cb7101",
			Description: "开心的文章描述_" + strconv.Itoa(i),
			Owner:       "系统管理员",
			Href:        "#",
			Star:        "星标",
			Like:        "喜欢",
			Message:     "信息留言_" + strconv.Itoa(i),
			Content:     "你想要的文章内容_" + strconv.Itoa(i),
			UpdateAt:    time.Now().Format("2006-01-02 15:04:05"),
		})
	}
	Success(ctx, gin.H{
		"list": list,
	})
}
